// 时令蔬菜
var __seasonItems = [ 15, 4 ];

$(function() {
	FastClick.attach(document.body);
	
	window.globalMarkets = null;
	window.globalCategories = null;
	// 所有菜场
	$('#el-markets').show();
	gmarkt.render(function(markets) {
		window.globalMarkets = markets;
	});
}());

function renderCommodities() {
	var market;
	if(window.globalMarkets) {
		market = window.globalMarkets[0];
	}
	if(!market) {
		return;
	}

	global__loading.start();
	// 加载蔬菜
	loadVegetables(market.marketId, function(obj) {

		window.globalCategories = obj.categories;

		renderSidebar();

		onClickSideTab(0);

		global__loading.stop();

	});
}

/**加载菜场下所有蔬菜*/
function loadVegetables(marketId, callback) {
	global__http.get('/market/' + marketId + '/items', null, function(resp) {
		if(!resp.success) {
			callback();
			return;
		}
		callback(resp.result);
	});
}

function renderSidebar() {
	var html = [
			'<li class="sidebar-tab-item sidebar-tab-0" onclick="onClickSideTab(0)">',
				'<div class="wrapper">日常菜品</div>',
			'</li>',
			'<li class="sidebar-tab-item sidebar-tab-1" onclick="onClickSideTab(1)">',
				'<div class="wrapper">时令菜品</div>',
			'</li>'
		].join('');
	$('#el-commodity-sidebar').html(html);
}

function renderCategory(index) {
	var html = '';

	if(index === 0) {
		globalCategories.forEach(function(item) {
			var commodities = item.commodities;
			commodities.forEach(function(c) {
				html += [
					'<li><a href="/commodity/' + c.commodityId + '?name=' + c.commodity + '">' + c.commodity + '</a></li>'
				].join('');
			});
		});
	} else {
		html += getSeasonItems();
	}

	$('#el-commodity-content').html(html);
}

function getSeasonItems() {
	var html = '';
	globalCategories.forEach(function(item) {
		var commodities = item.commodities;
		commodities.forEach(function(c) {
			for(var i=0; i<__seasonItems.length; i++) {
				if(c.commodityId === __seasonItems[i]) {
					html += [
						'<li><a href="/commodity/' + c.commodityId + '?name=' + c.commodity + '">' + c.commodity + '</a></li>'
					].join('');
					break;
				}
			}
		});
	});
	return html;
}

/******************************************************************/
function onClickNavTab(index) {
	$('.nav-tab-item .arrow').removeClass('active');
	$('.nav-tab-' + index + ' .arrow').addClass('active');
	$('#el-markets').hide();
	$('#el-commodities').hide();

	if(index === 0) {
		// 菜
		$('#el-commodities').show();
		if($('#el-commodity-sidebar li').length === 0) {
			renderCommodities();
		}
	} else {
		// 菜场
		$('#el-markets').show();
	}
}


function onSearch() {
	var v = $('#el-header input').val();
	var html = '';
	// console.log(window.markets);
	window.markets.forEach(function(m) {
		if(!v || m.marketName.indexOf(v) !== -1) {
			html += [
				'<li class="market-item">',
					'<a href="/market/' + m.marketId + '">',
						'<div class="name">' + m.marketName + '</div>',
						'<div class="distance">' + m.distance + '</div>',
					'</a>',
				'</li>'
			].join('');
		}
	});

	$('#el-commodities').hide();
	$('#el-markets').show();
	$('.nav-tab-item .arrow').removeClass('active');
	$('.nav-tab-1 .arrow').addClass('active');

	$('#el-markets').html(html);
}